#pragma once
#include "event2/http.h"
#include "event2/event.h"
#include "event2/buffer.h"
#include "event2/keyvalq_struct.h"
#include <evhttp.h>
#include <event2/thread.h>

#include "file_server.h"
#include "photo_file_server.h"
#include "continuous_shooting_file_server.h"
#include "panorama_file_server.h"


int start_http_album_server(unsigned int port) {
#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);

    evthread_use_windows_threads();
#else
    evthread_use_pthreads();
#endif

    struct event_base* base = event_base_new();
    if (!base) {
        printf("create event_base failed\n");
    }
    struct evhttp* http = evhttp_new(base);
    if (!http) {
        printf("create evhttp failed\n");
    }
    if (evhttp_bind_socket(http, "0.0.0.0", port) != 0) {
        printf("bind socket failed\n");
    }

    FileServerOption option;
    
    option.base_dirctory = "/sdcard";
    option.base_url = "http://localhost:8081/";
    option.enable_directory_search = true;
    option.exts.emplace_back(".html");
    option.exts.emplace_back(".jpg");
	option.exts.emplace_back(".jpeg");
    option.exts.emplace_back(".png");

    FileServer server(option);

    PhotoFileServerOption photo_option;
    photo_option.base_dirctory = "/sdcard/photo";
    photo_option.base_url = "http://localhost:8081/";
    photo_option.enable_directory_search = true;
    photo_option.enable_thumbnail = true;
    photo_option.thumbnail_directory = "/sdcard/thumail";
    photo_option.thumbnail_url = "http://localhost:8081/thumail/";
    photo_option.exts.emplace_back(".jpg");
    photo_option.exts.emplace_back(".png");

    PhotoFileServer photofileserver(photo_option);

    ContinuousShootingFileServerOption continuousshooting_option;
    continuousshooting_option.base_dirctory = "/sdcard/csphoto";
    continuousshooting_option.base_url = "http://localhost:8081/";
    continuousshooting_option.enable_directory_search = true;
    continuousshooting_option.enable_thumbnail = true;
    continuousshooting_option.exts.emplace_back(".jpg");
    continuousshooting_option.exts.emplace_back(".png");
    continuousshooting_option.thumbnail_directory = "/sdcard/thumail";
    continuousshooting_option.thumbnail_url = "http://localhost:8081/thumail/";

    ContinuousShootingFileServer continuousshootingfileserver(continuousshooting_option);

    PanoFileServerOption panofileserveroption;
    panofileserveroption.base_dirctory = "/sdcard/pano";
    panofileserveroption.base_url = "http://localhost:8081/";
    panofileserveroption.enable_directory_search = true;
    panofileserveroption.exts.emplace_back(".jpg");

    PanoramaFileServer panofileserver(panofileserveroption);

    evhttp_set_cb(http, "/QueryFiles", [](evhttp_request* req,void* arg){
        ((FileServer*)arg)->QueryServerFiles(req);
    }, &server);
    evhttp_set_cb(http, "/QueryDirs", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->QueryServerDirs(req);
        }, &server);
    evhttp_set_cb(http, "/RenameFile", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->RenameServerFile(req);
        }, &server);
    evhttp_set_cb(http, "/DeleteFile", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->DeleteServerFile(req);
        }, &server);

    evhttp_set_cb(http, "/QueryPhotoFiles", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->QueryServerFiles(req);
        }, & photofileserver);
    evhttp_set_cb(http, "/QueryPhotoDirs", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->QueryServerDirs(req);
        }, & photofileserver);
    evhttp_set_cb(http, "/RenamePhotoFile", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->RenameServerFile(req);
        }, & photofileserver);
    evhttp_set_cb(http, "/DeletePhotoFile", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->DeleteServerFile(req);
        }, & photofileserver);

    evhttp_set_cb(http, "/QueryContinuousShootingFile", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->QueryServerFiles(req);
            }, &continuousshootingfileserver);
    evhttp_set_cb(http, "/QueryContinuousShootingDirs", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->QueryServerDirs(req);
        }, &continuousshootingfileserver);
    evhttp_set_cb(http, "/RenameContinuousShootingFile", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->RenameServerFile(req);
        }, &continuousshootingfileserver);
    evhttp_set_cb(http, "/DeleteContinuousShootingFile", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->DeleteServerFile(req);
        }, &continuousshootingfileserver);

    evhttp_set_cb(http, "/QueryPanoFiles", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->QueryServerFiles(req);
        }, & panofileserver);
    evhttp_set_cb(http, "/QueryPanoDirs", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->QueryServerDirs(req);
        }, & panofileserver);
    evhttp_set_cb(http, "/RenamePanoFile", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->RenameServerFile(req);
        }, & panofileserver);
    evhttp_set_cb(http, "/DeletePanoFile", [](evhttp_request* req, void* arg) {
        ((FileServer*)arg)->DeleteServerFile(req);
        }, & panofileserver);

    event_base_dispatch(base);

	return 0;
}